#include <cmath>
#include <iostream>

constexpr double JINGDU = 0.00001;

class Solution {
public:
    double mySqrt(int x) {
        double high = x;
        double low = 0;
        double mid = (high + low) / 2.0;
        double target = mid * mid;
        while (target - x > JINGDU or target - x < -JINGDU) {
            std::cout << target << ", " << mid << '\n';
            if (target > x) {
                high = mid;
            } else {
                low = mid;
            }
            mid = (high + low) / 2.0;
            target = mid * mid;
        }
        std::cout << target << ", " << mid << '\n';
        int result = static_cast<int>(mid);
        if (result * result > x)
            return result - 1;
        else if ((result + 1) * (1 + result) <= x)
            return result + 1;
        else
            return result;
    }
};

int main() {
    Solution s;
    std::cout << s.mySqrt(4) << '\n';
}